package com.pig4cloud.pigx.wallet.mapstruct;

import com.pig4cloud.pigx.common.customize.i18n.util.MessageUtils;
import com.pig4cloud.pigx.common.data.resolver.DictResolver;
import com.pig4cloud.pigx.wallet.constant.I18nMessages;
import com.pig4cloud.pigx.wallet.constant.WalletStatusEnum;
import com.pig4cloud.pigx.wallet.dto.ChargeMentionNotificationDTO;
import com.pig4cloud.pigx.wallet.entity.Notification;
import com.pig4cloud.pigx.wallet.vo.MobileNotificationVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Description: 用户消息通知
 * @Date: 2022/10/21 16:44
 */
@Mapper
public interface NotificationStruct {

    NotificationStruct INSTANCE = Mappers.getMapper(NotificationStruct.class);

    /**
     * 用户消息通知 转 移动端消息通知
     *
     * @param notification
     * @return
     */
    MobileNotificationVO notification2MobileVO(Notification notification);

    /**
     * 用户消息通知集合 转 移动端消息通知集合
     *
     * @param notificationList
     * @return
     */
    List<MobileNotificationVO> notification2MobileVO(List<Notification> notificationList);


    /**
     * 充提记录 转 用户消息通知
     *
     * @param chargeMentionNotificationDTO
     * @return
     */
    @Mapping(target = "id", ignore = true)
    @Mapping(source = "id", target = "recordId")
    @Mapping(source = "chargeMentionNotificationDTO", target = "title", qualifiedByName = "titleAssembly")
    @Mapping(source = "chargeMentionNotificationDTO", target = "message", qualifiedByName = "messageAssembly")
    Notification chargeMentionDTO2PO(ChargeMentionNotificationDTO chargeMentionNotificationDTO);


    /**
     * 组装通知标题参数
     *
     * @param dto
     * @return
     */
    @Named("titleAssembly")
    static String titleAssembly(ChargeMentionNotificationDTO dto) {
        String title = null;
        if (dto.getNoticeType().equals(WalletStatusEnum.WalletNotice.RECHARGE.getCode())) {
            // 充值
            title = MessageUtils.getMessageByLocale(I18nMessages.NOTICE_THE_TITLE, MessageUtils.getLocale(dto.getLanguage()),
                    // 类型
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("user_fund_recharge_type", dto.getType()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null),
                    // 状态
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("recharge_state", dto.getState()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null)
            );
        } else if (dto.getNoticeType().equals(WalletStatusEnum.WalletNotice.WITHDRAWAL.getCode())) {
            // 提现
            title = MessageUtils.getMessageByLocale(I18nMessages.NOTICE_THE_TITLE, MessageUtils.getLocale(dto.getLanguage()),
                    // 类型
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("wallet_notice", dto.getNoticeType()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null),
                    // 状态
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("withdrawal_state", dto.getState()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null)
            );
        }

        return title;
    }

    /**
     * 组装通知内容参数
     *
     * @param dto
     * @return
     */
    @Named("messageAssembly")
    static String messageAssembly(ChargeMentionNotificationDTO dto) {
        String message = null;
        if (dto.getNoticeType().equals(WalletStatusEnum.WalletNotice.RECHARGE.getCode())) {
            // 充值
            message = MessageUtils.getMessageByLocale(I18nMessages.NOTIFICATION_MESSAGE, MessageUtils.getLocale(dto.getLanguage()),
                    // 类型
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("user_fund_recharge_type", dto.getType()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null),
                    // 数量
                    dto.getQuantity(),
                    // 币种名
                    DictResolver.getDictItemByItemValueInner("currency_name", dto.getCurrency()).getLabel(),
                    // 时间
                    dto.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                    // 状态
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("recharge_state", dto.getState()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null)
            );
        } else if (dto.getNoticeType().equals(WalletStatusEnum.WalletNotice.WITHDRAWAL.getCode())) {
            // 提现
            message = MessageUtils.getMessageByLocale(I18nMessages.NOTIFICATION_MESSAGE, MessageUtils.getLocale(dto.getLanguage()),
                    // 类型
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("wallet_notice", dto.getNoticeType()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null),
                    // 数量
                    dto.getQuantity(),
                    // 币种名
                    DictResolver.getDictItemByItemValueInner("currency_name", dto.getCurrency()).getLabel(),
                    // 时间
                    dto.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")),
                    // 状态
                    MessageUtils.getMessageByLocale(DictResolver.getDictItemByItemValueInner("withdrawal_state", dto.getState()).getRemarks(), MessageUtils.getLocale(dto.getLanguage()), null)
            );
        }
        return message;
    }
}
